package com.wing.product.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.Product;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.service.ProductDetailService;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "App-产品明细表管理", tags = "产品明细表管理")
@RestController("appProductDetailController")
@RequestMapping("product/app/productDetail")
public class ProductDetailController {

    @Autowired
    private ProductDetailService productDetailService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询所有产品SKU", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductDetail> list(
            @RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "limit", defaultValue = "10") Integer limit
            , @RequestParam(name = "sort", defaultValue = "id") String sort
            , @RequestParam(name = "order", defaultValue = "desc") String order
            , String skuCode
            , Long productTypeId
            , Long productCategoryId
            , String skuName
            , Long categoryId
            , String codes
            , String productCategoryIds
            , String skuSpecValueNames
            , String queryFlag
            , String cabCode
            , String searchValue) {
        Page<ProductDetail> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", StrUtil.toUnderlineCase(order));
        paramMap.put("skuCode", skuCode);
        paramMap.put("productTypeId", productTypeId);
        paramMap.put("productCategoryId", productCategoryId);
        paramMap.put("skuSpecValueNames", skuSpecValueNames);
        //搜索
        if (StringUtils.isNotBlank(skuName)) {
            paramMap.put("skuName", skuName);
        }
        if (null != categoryId) {
            paramMap.put("categoryId", categoryId);
        }
        if (null != codes) {
            paramMap.put("codes", codes);
        }
        if (StringUtils.isNotBlank(productCategoryIds)) {
            List<String> sList = Arrays.asList(productCategoryIds.split(","));
            paramMap.put("productCategoryIds", sList);
        }
        if (null != codes) {
            paramMap.put("codes", codes);
        }
        paramMap.put("queryFlag", queryFlag);
        paramMap.put("cabCode", cabCode);
        if (StringUtils.isNotBlank(searchValue)) {
            paramMap.put("searchValue", searchValue);
        }
        IPage<ProductDetail> list = productDetailService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long productDetailId) {
        ProductDetail productDetail = productDetailService.get4appById(productDetailId);
        if (productDetail == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), productDetail);
    }

    @PostMapping()
    public JsonResult add(@Validated ProductDetail productDetail) {
        if (productDetailService.save(productDetail)) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    /**
     * 对外开放的API接口列表(产品明细)
     *
     * @param page
     * @param limit
     * @param skuName
     * @return
     */
    @GetMapping("/foreignProductInfoListAll")
    public PageResult<ProductDetail> foreignProductInfoListAll(Integer page, Integer limit, String skuName, String skuCode, Long productId, String startTime, String endTime) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        Page<Product> productDetailPage = new Page<>(page, limit);
        Map paramMap = new HashMap();
        paramMap.put("skuName", skuName);
        paramMap.put("skuCode", skuCode);
        paramMap.put("productId", productId);
        paramMap.put("startCreateTime", startTime);
        paramMap.put("endCreateTime", endTime);
        Page<ProductDetail> pageList = productDetailService.pageList4app(productDetailPage, paramMap);
        List<ProductDetail> userList = pageList.getRecords();
        return new PageResult<>(userList, pageList.getTotal());
    }

    /**
     * 对外开放的API接口详情(产品明细)
     *
     * @param id
     * @return
     */
    @GetMapping("/foreignProductInfoDetail")
    public JsonResult foreignProductInfoDetail(Long id) {
        ProductDetail productDetail = productDetailService.getById(id);
        return JsonResult.success(productDetail);
    }
}
